﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;

namespace ZHDJ.BranchCenter.Module.MassLine
{
    public partial class ActivityBulletinEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle model = DB.PartyArticles.Find(id);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                this.txtTitle.Text = model.Title;
                this.heContent.Text = model.Content;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyArticle model = DB.PartyArticles.Find(id);
                if (model == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                model.Title = this.txtTitle.Text;
                model.Content = this.heContent.Text;
                model.ModifyTime = DateTime.Now;
                model.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = txtTitle.Text.Trim();
                article.Module = "MassLine";
                article.Category = "ActivityBulletin";
                article.Status = 4;
                if (string.IsNullOrEmpty(heContent.Text.Trim()))
                {
                    Alert.Show("内容不能为空！", String.Empty);
                    return;
                }
                article.Content = heContent.Text;
                article.BranchID = CurrentUser.BranchID;
                article.CreateTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;
                DB.PartyArticles.Add(article);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        #endregion
    }
}